package ru.tapublog.lib.samsungwallet.impl.converter;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

import ru.tapublog.lib.samsungwallet.api.model.AbstractViewElement;
import ru.tapublog.lib.samsungwallet.api.model.Barcode;
import ru.tapublog.lib.samsungwallet.api.model.IMAGE_TYPE;
import ru.tapublog.lib.samsungwallet.api.model.Image;
import ru.tapublog.lib.samsungwallet.api.model.SolidColorImage;
import ru.tapublog.lib.samsungwallet.api.model.Text;
import ru.tapublog.lib.samsungwallet.impl.dao.BarcodeDAO;
import ru.tapublog.lib.samsungwallet.impl.dao.ImageDAO;
import ru.tapublog.lib.samsungwallet.impl.dao.TextElementData;
import ru.tapublog.lib.samsungwallet.impl.dao.ViewElement;

public class ViewElementConverter {
	public static ViewElement convert(AbstractViewElement element) {
		if (element instanceof Text)
			return convertText((Text) element);
		else if (element instanceof Image)
			return convertImage((Image) element);
		else if (element instanceof SolidColorImage)
			return convertSolidColorImage((SolidColorImage) element);
		else if (element instanceof Barcode)
			return convertBarcode((Barcode) element);

		throw new IllegalArgumentException("Unsupported type");
	}

	public static String convertColor(byte[] color) {
		if (color == null)
			return null;
		return String.format("#%s", new String(Hex.encodeHex(color)));
	}

	public static byte[] convertColor(String color) throws DecoderException {
		return Hex.decodeHex(color.substring(1, color.length()).toCharArray());
	}

	public static String convertImageType(IMAGE_TYPE imageType) {
		return imageType.toString().toLowerCase() + ";base64";
	}

	public static ViewElement convertText(Text element) {
		ViewElement viewElement = new ViewElement();
		viewElement.setId(element.getId());
		TextElementData textElement = new TextElementData();
		textElement.setAlign(element.getAlign());
		textElement.setColor(convertColor(element.getColor()));
		textElement.setValue(element.getValue());
		viewElement.setText(textElement);
		return viewElement;
	}

	public static ViewElement convertImage(Image element) {
		ViewElement viewElement = new ViewElement();
		viewElement.setId(element.getId());
		ImageDAO imageElement = new ImageDAO();
		imageElement.setAlign(element.getAlign());
		imageElement.setType(convertImageType(element.getImageType()));
		imageElement.setValue(element.getValue());
		viewElement.setImage(imageElement);
		return viewElement;
	}

	public static ViewElement convertSolidColorImage(SolidColorImage element) {
		ViewElement viewElement = new ViewElement();
		viewElement.setId(element.getId());
		ImageDAO imageElement = new ImageDAO();
		imageElement.setBgColor(convertColor(element.getBackgroundColor()));
		viewElement.setImage(imageElement);
		return viewElement;
	}

	public static ViewElement convertBarcode(Barcode element) {
		ViewElement viewElement = new ViewElement();
		viewElement.setId(element.getId());
		BarcodeDAO barcodeElement = new BarcodeDAO();
		barcodeElement.setType(element.getBarcodeType().toString());
		barcodeElement.setCaption(element.getCaption());
		barcodeElement.setValue(element.getValue());
		viewElement.setBarcode(barcodeElement);
		return viewElement;
	}
}
